package com.qiqi.admin.basicdata.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiqi.basicdata.dto.ProductTypeDTO;
import com.qiqi.basicdata.entity.ProductTypeDO;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiqi.common.entity.PageEntity;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.qiqi.basicdata.service.ProductTypeService;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Queue;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiQiDream
 * @since 2020-11-28
 */
@Api("产品类型相关接口")
@RestController
@RequestMapping("/product/type")
public class ProductTypeController {

    @Resource
    private ProductTypeService productTypeService;

    @ApiOperation(value = "获取(列表)")
    @PostMapping("/list")
    public PageEntity<ProductTypeDO> getProductTypePage(@RequestBody ProductTypeDTO productType) {
        LambdaQueryWrapper<ProductTypeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(productType.getCode()),ProductTypeDO::getCode,productType.getCode())
                .like(StringUtils.isNotBlank(productType.getName()),ProductTypeDO::getName,productType.getName());
        IPage<ProductTypeDO> iPage = productTypeService.page(new Page<>(productType.getPage(),productType.getCount()),queryWrapper);
        return new PageEntity<>(iPage.getTotal(),Convert.convert(new TypeReference<List<ProductTypeDO>>() {}, iPage.getRecords()));
    }

    @ApiOperation(value = "获取产品类型不分页")
    @GetMapping("/all")
    public List<ProductTypeDO> getList(){
        return productTypeService.list();
    }
    @ApiOperation(value = "获取(单个)")
    @GetMapping("/{id}")
    public ProductTypeDO getProductType(@PathVariable Long id) {
        return productTypeService.getById(id);
    }

    @ApiOperation(value = "新增")
    @PostMapping("add")
    public Boolean saveProductType(@RequestBody ProductTypeDO productTypeDO) {
        return productTypeService.saveOrUpdate(productTypeDO);
    }

    @ApiOperation(value = "删除(单个))")
    @DeleteMapping("/{id}")
    public Boolean deleteProductTypeById(@PathVariable Long id) {
        return productTypeService.removeById(id);
    }
}
